package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Dwdb;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class DwdbForm extends BasePage implements Serializable {
    private GenericManager<Dwdb, Long> dwdbManager;
    private Dwdb dwdb = new Dwdb();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setDwdbManager(GenericManager<Dwdb, Long> dwdbManager) {
        this.dwdbManager = dwdbManager;
    }

    public Dwdb getDwdb() {
        return dwdb;
    }

    public void setDwdb(Dwdb dwdb) {
        this.dwdb = dwdb;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        dwdbManager.remove(dwdb.getId());
        addMessage("dwdb.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            dwdb = dwdbManager.get(id);
        } else {
            dwdb = new Dwdb();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (dwdb.getId() == null || dwdb.getId() == 0);
        dwdb.setJbqk(jbqk);
     // set username who act this operation, get the user name from httprequest
        dwdb.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        dwdb.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        dwdb.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        dwdbManager.save(dwdb);

        String key = (isNew) ? "dwdb.added" : "dwdb.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	// set the id to the next page
            this.id = dwdb.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 